package com.example.zx.theline.lyjh.lyrjh;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.zx.theline.R;

import Utils.BackHandlerHelper;
import app.MyApplication;
import base.BaseActivity;
import com.example.zx.theline.lyjh.lyrjh.SgrjhCxFragment;
import com.example.zx.theline.lyjh.lyrjh.SgrjhShFragment;
import com.example.zx.theline.lyjh.lyrjh.SgrjhShywcFragment;

/**
 * Created by Jianqi on 2017\8\24 0024.
 * <p>
 * 邻营日计划主界面
 */

public class SgrjhActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup mRadioGroup;
    private Fragment mShrjhFgt, mShywcFgt, mCxrjhFgt;
    private RadioButton mShrjhRbt, mShywcRbt, mCxrjhRbt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_plan);
        InitView();
        InitListen();
        InitData();
    }

    @Override
    protected void InitView() {
        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        mShrjhRbt = (RadioButton) findViewById(R.id.sh_rjh);
        mShywcRbt = (RadioButton) findViewById(R.id.sh_ywc);
        mCxrjhRbt = (RadioButton) findViewById(R.id.cx_rjh);
    }

    @Override
    protected void InitData() {
        //分不同情况加载
        if (MyApplication.getmLoginUser().getSgrjh().isShrjh() && MyApplication.getmLoginUser().getSgrjh().isShywc() && MyApplication.getmLoginUser().getSgrjh().isRjhcx()) {
            displayView(mShrjhRbt, 0, 0, 0, 0);
        } else if (MyApplication.getmLoginUser().getSgrjh().isShrjh() && MyApplication.getmLoginUser().getSgrjh().isShywc() && !MyApplication.getmLoginUser().getSgrjh().isRjhcx()) {
            displayView(mShrjhRbt, 0, 0, 8, 0);
        } else if (MyApplication.getmLoginUser().getSgrjh().isShrjh() && !MyApplication.getmLoginUser().getSgrjh().isShywc() && MyApplication.getmLoginUser().getSgrjh().isRjhcx()) {
            displayView(mShrjhRbt, 0, 8, 0, 0);
        } else if (!MyApplication.getmLoginUser().getSgrjh().isShrjh() && MyApplication.getmLoginUser().getSgrjh().isShywc() && MyApplication.getmLoginUser().getSgrjh().isRjhcx()) {
            displayView(mShywcRbt, 8, 0, 0, 1);
        } else if (!MyApplication.getmLoginUser().getSgrjh().isShrjh() && !MyApplication.getmLoginUser().getSgrjh().isShywc() && MyApplication.getmLoginUser().getSgrjh().isRjhcx()) {
            displayView(mCxrjhRbt, 8, 8, 0, 2);
        } else if (!MyApplication.getmLoginUser().getSgrjh().isShrjh() && MyApplication.getmLoginUser().getSgrjh().isShywc() && !MyApplication.getmLoginUser().getSgrjh().isRjhcx()) {
            displayView(mShywcRbt, 8, 0, 8, 1);
        } else if (MyApplication.getmLoginUser().getSgrjh().isShrjh() && !MyApplication.getmLoginUser().getSgrjh().isShywc() && !MyApplication.getmLoginUser().getSgrjh().isRjhcx()) {
            displayView(mShrjhRbt, 0, 8, 8, 0);
        }
    }

    @Override
    protected void InitListen() {
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.sh_rjh:
                setSelect(0);
                break;
            case R.id.sh_ywc:
                setSelect(1);
                break;
            case R.id.cx_rjh:
                setSelect(2);
                break;
        }
    }

    //控制界面中按钮的显示及默认选中状态
    private void displayView(RadioButton mRadioBtn, int a, int b, int c, int d) {
        mShrjhRbt.setVisibility(a);
        mShywcRbt.setVisibility(b);
        mCxrjhRbt.setVisibility(c);
        mRadioBtn.setChecked(true);
        setSelect(d);
    }

    //Fragment的选择事件
    private void setSelect(int i) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                //审核日计划显示
                if (MyApplication.getmLoginUser().getSgrjh().isShrjh()) {
                    mShrjhRbt.setVisibility(View.VISIBLE);
                    if (mShrjhFgt == null) {
                        mShrjhFgt = new SgrjhShFragment();
                        transaction.add(R.id.framelayout, mShrjhFgt);
                    }
                } else {
                    mShrjhRbt.setVisibility(View.GONE);
                }
                transaction.show(mShrjhFgt);
                break;
            case 1:
                //审核已完成显示
                if (MyApplication.getmLoginUser().getSgrjh().isShywc()) {
                    mShywcRbt.setVisibility(View.VISIBLE);
                    if (mShywcFgt == null) {
                        mShywcFgt = new SgrjhShywcFragment();
                        transaction.add(R.id.framelayout, mShywcFgt);
                    }
                } else {
                    mShywcRbt.setVisibility(View.GONE);
                }
                transaction.show(mShywcFgt);
                break;
            case 2:
                //审核日计划查询
                if (MyApplication.getmLoginUser().getSgrjh().isRjhcx()) {
                    mCxrjhRbt.setVisibility(View.VISIBLE);
                    if (mCxrjhFgt == null) {
                        mCxrjhFgt = new SgrjhCxFragment();
                        transaction.add(R.id.framelayout, mCxrjhFgt);
                    }
                } else {
                    mCxrjhRbt.setVisibility(View.GONE);
                }
                transaction.show(mCxrjhFgt);
                break;
        }
        transaction.commit();
    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (mShrjhFgt != null) {
            transaction.hide(mShrjhFgt);
        }
        if (mShywcFgt != null) {
            transaction.hide(mShywcFgt);
        }
        if (mCxrjhFgt != null) {
            transaction.hide(mCxrjhFgt);
        }
    }

    //处理系统返回键
    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            super.onBackPressed();
        }
    }

}
